#include<stdio.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>


//child->write,father->read 
int main()
{
  int fd[2] = {0};
  if(pipe(fd)<0){
    perror("pipe!");
    return 1;
  }
  
  //printf("fd[0]: %d\n",fd[0]);
  //printf("fd[1]: %d\n",fd[1]);
  
  pid_t id  = fork();
  if(id == 0){
    //child
    close(fd[0]);
    const char* msg = "hello father, I am child!";
    int count = 30;
    while(count){
      if(count == 2){
        sleep(1000);
      }
      else{
        sleep(1);
        write(fd[1], msg, strlen(msg));
        count--;
      }
      //sleep(1);
    }
    close(fd[1]);
    exit(0);
  }

  close(fd[1]);
  char buff[64];
  while(1){
    ssize_t s = read(fd[0], buff, sizeof(buff));
    if(s > 0){
      buff[s] = '\0';
      printf("child send to father# %s\n",buff);
    }
    else if(s == 0){
      printf("read file end!\n");
      break;
    }
    else{
      printf("read error!\n");
      break;
    }
    close(fd[0]);
    break;
  }

  sleep(5);
  //father
  int status = 0;
  waitpid(id,&status,0);

  printf("child quit!: sig: %d\n", status & 0x7F);

  return 0;
}




